﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebHoiChoMuaBan.Models;
using System.Text;

namespace WebHoiChoMuaBan.Controllers.Helpers
{
    public class ChatHelper : HelperBase
    {
        public void AddMsg(int memberId, string msg)
        {
            MemberHelper hMem = new MemberHelper();
            Member mem = hMem.getMember(memberId);
            if (mem != null && msg != null && msg.Length > 0)
            {
                ChatHistory chat = new ChatHistory();
                chat.chatTime = DateTime.Now;
                chat.chatMsg = msg;
                mem.ChatHistories.Add(chat);
                hMem.Save();
            }
        }

        public string GetHistory()
        {
            List<ChatHistory> his = (from h in this.dataContext.ChatHistories
                                     orderby h.chatTime descending
                                     select h).Take(25).ToList<ChatHistory>();
            StringBuilder sb = new StringBuilder();
            foreach (ChatHistory h in his)
            {
                sb.Append("<p><span class='private_price'>");
                sb.Append(h.Member.MemUsername).Append(":</span> ");
                sb.Append(h.chatMsg).Append("</p>");
            }
            return sb.ToString();
        }
        /// <summary>
        /// get EmotionPath
        /// </summary>
        /// <returns></returns>
        public List<ChatEmotionDetail> getEmotionPath()
        {
            ChatEmotion emo = (from e in this.dataContext.ChatEmotions where e.Status == 1 select e).FirstOrDefault();
            List<ChatEmotionDetail> emodetail = (from e in this.dataContext.ChatEmotionDetails where e.catId == emo.Id select e).ToList<ChatEmotionDetail>();
            foreach (ChatEmotionDetail e in emodetail)
            {
                e.path = emo.Path + e.path;
            }
            return emodetail;
        }

        public void AddMsgToMember(int memidSend, int memidRe,int? comIdReply,string comContent)
        {
            CommentMember com = new CommentMember();
            com.ComContent = comContent;
            com.ComDate = DateTime.Now;
            com.ComIdReply = comIdReply;
            com.MemIdSend = memidSend;
            com.MemIdReceive = memidRe;
            this.dataContext.CommentMembers.InsertOnSubmit(com);
            this.dataContext.SubmitChanges();
        }
    }
}